package com.CmJava.crowd.service.imp;

import com.CmJava.crowd.entity.ResultEntity;
import com.CmJava.crowd.entity.*;
import com.CmJava.crowd.mapper.GoodMapper;
import com.CmJava.crowd.mapper.UserAndGoodMapper;
import com.CmJava.crowd.mapper.UserMapper;
import com.CmJava.crowd.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAndGoodMapper userAndGoodMapper;

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<User> userList(int cp, int pageNum, String keyWord) {

        // 开启分页功能
        PageHelper.startPage(cp,pageNum);
        List<User> users = userMapper.userList(keyWord);
        // 封装到PageInfo中
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return userPageInfo;

    }

    @Override
    public ResultEntity userDetail(String primaryKey) {
        User user;
        try{
            user = userMapper.selectByPrimaryKey(Integer.parseInt(primaryKey));
        }catch (Exception e ){
            return ResultEntity.failWithoutMessage();
        }
        if (user!=null) {
            return ResultEntity.successWithData(user);
        }else {
            return ResultEntity.failWithoutMessage();
        }
    }

    @Override
    public ResultEntity getUserGoods(String primaryKey) {
        UserAndGoodExample userAndGoodExample = new UserAndGoodExample();
        UserAndGoodExample.Criteria criteria = userAndGoodExample.createCriteria();
        criteria.andUseridEqualTo(Integer.parseInt(primaryKey));
        List<UserAndGood> userAndGoods = userAndGoodMapper.selectByExample(userAndGoodExample);
        if (userAndGoods==null || userAndGoods.size()==0){
            return ResultEntity.successWithoutData();
        }

        // 该用户拥有的项目id列表
        List<Integer> idList = new ArrayList<>();
        for (UserAndGood userAndGood : userAndGoods) {
            idList.add(userAndGood.getGoodid());
        }

        // 查询该id列表下的项目信息
        GoodExample goodExample = new GoodExample();
        GoodExample.Criteria criteria1 = goodExample.createCriteria();
        criteria1.andIdIn(idList);
        List<Good> goods = goodMapper.selectByExample(goodExample);
        if (goods!=null && goods.size()>0) {
            return ResultEntity.successWithData(goods);
        }else {
            return ResultEntity.failWithoutMessage();
        }
    }

    @Override
    public ResultEntity getTheseGoodRedisInfo(List<String> goodId) {
        HashMap<String, FundMoneyBy14Days.DateAndMoney> fundMoneyResult=new HashMap<>();
        for (String id : goodId) {
            FundMoneyBy14Days fundMoneyBy14Days = (FundMoneyBy14Days) redisTemplate.opsForValue().get(id + "-14Days");
            if (fundMoneyBy14Days!=null ){
                ArrayList<FundMoneyBy14Days.DateAndMoney> fundMoney = fundMoneyBy14Days.getFundMoney();
                int size = fundMoney.size();
                if (size==1){
                    FundMoneyBy14Days.DateAndMoney dateAndMoney = fundMoney.get(0);
                    Set<String> keySet = dateAndMoney.keySet();
                    for (String date : keySet) {
                        Integer money = dateAndMoney.get(date);
                        if (money!=0){
                            fundMoneyResult.put(id,dateAndMoney);
                        }
                    }
                }else {
                    for (int i = size-1; i >=0 ; i--) {
                        Integer money=0;
                        FundMoneyBy14Days.DateAndMoney dateAndMoney = fundMoney.get(i);
                        Set<String> keySet = dateAndMoney.keySet();
                        for (String date : keySet) {
                            money = dateAndMoney.get(date);
                        }
                        if (money==0){
                            continue;
                        }else {
                            fundMoneyResult.put(id,dateAndMoney);
                        }
                    }
                }
            }
        }
        return ResultEntity.successWithData(fundMoneyResult);
    }

    @Override
    public ResultEntity deleteUser(String primaryKey) {
        int i = userMapper.deleteByPrimaryKey(Integer.parseInt(primaryKey));
        if (i>0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failWithoutMessage();
        }
    }

    @Override
    public ResultEntity updateThisUser(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failWithoutMessage();
        }
    }

    @Override
    public ResultEntity examineThisUser(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failWithoutMessage();
        }
    }
}
